#include <glad/glad.h>
#include "UniformShader.h"


UniformShader::UniformShader()
{
    this->vertexShaderSource =
        "#version 330 core\n"
        "layout (location = 0) in vec3 aPos;\n"
        //  "out vec4 vertexColor;\n"   // 顶点着色器中声明输出颜色
        "void main()\n"
        "{\n"
        // gl_Position 作为顶点着色器的数据，要求是一个四维向量，并且是符合
        // Normalized Device Coordinates (NDC) 类型
        // 因为这个demo的输入就是符合Normalized Device Coordinates
        // (NDC)，所以这里直接转换为四维向量即可
        "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
        //  "   vertexColor = vec4(0.5, 0, 0.2, 1.0);\n"
        "}\0";

    this->fragmentShaderSource =
        "#version 330 core\n"
        "out vec4 FragColor;\n"
        //  "in  vec4 vertexColor;\n"    //
        //  片段着色器中声明接受顶点着色器中输出的顶点颜色
        "uniform  vec4 vertexColor;\n"
        "void main()\n"
        "{\n"
        "FragColor = vertexColor;\n"
        "}\0";
}

unsigned int UniformShader::createShaderProgram()
{
    ShaderProgram::createShaderProgram();

    /**
    we query for the location of the ourColor uniform using
    glGetUniformLocation. We supply the shader program and the name of the
    uniform (that we want to retrieve the location from) to the query function
    */
    vertexColorLocation = glGetUniformLocation(shaderProgram, "vertexColor");

    return shaderProgram;
}

void UniformShader::update()
{

    float time = glfwGetTime();

    float greenValue = sin(time) / 2.0f + 0.5f;
    /**
    Note that finding the uniform location does not require you to use the
    shader program first, but updating a uniform does require you to first
    use the program (by calling glUseProgram), because it sets the uniform
    on the currently active shader program.
    因为GLSL不支持函数重载，所以需要用后缀"4f"来指定uniform的参数类型
    */
    glUniform4f(vertexColorLocation, 0, greenValue, 0, 1);
}